﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace SNRepairTools
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string mac = GetMacAddress();
            Console.WriteLine("MAC: " + mac);
            if (string.IsNullOrEmpty(mac))
            {
                Console.WriteLine("未能获取到MAC地址");
                return;
            }

            ChangeIni(mac);

            ChangedMysql(mac);

            Console.WriteLine("修改完成");
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }


        private static string GetMacAddress()
        {
            string mac = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    mac = mo["MacAddress"].ToString();
                    break;
                }
            }

            return mac;


            string macAddress = string.Empty;
            try
            {
                var nic = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                    .FirstOrDefault(n => n.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up &&
                                         n.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback);
                if (nic != null)
                {
                    macAddress = nic.GetPhysicalAddress().ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取MAC地址失败：" + ex.Message);
            }
            return macAddress;
        }


        private static void ChangeIni(string newMac)
        {
            string oldMac = IniHelper.ProfileReadValue("Sys", "mac", IniHelper.parameterSetting);
            Console.WriteLine("旧MAC地址: " + oldMac);

            IniHelper.ProfileWriteValue("Sys", "mac", newMac, IniHelper.parameterSetting);

            string mac = IniHelper.ProfileReadValue("Sys", "mac", IniHelper.parameterSetting);
            if (mac == newMac)
            {
                Console.WriteLine("mac 修改成功");
            }
            else
            {
                Console.WriteLine("mac 修改失败");
            }
        }



        private static void ChangedMysql(string mac)
        {
            string connStr = "server=127.0.0.1;port=3307;user=root;password=qmai@2021;database=qmai;";

            string updateSql = $"update qm_pos_devices set ip_addr = '{mac}' where  pos_num = 'SP9999' and is_deleted = 0;";

            try
            {
                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();

                    using (MySqlCommand cmd = new MySqlCommand(updateSql, conn))
                    {
                        int rowsAffected = cmd.ExecuteNonQuery();
                        Console.WriteLine($"更新成功，影响行数：{rowsAffected}");
                    }

                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("执行出错：" + ex.Message);
            }

        }
    }
}
